const keywords = [
    'alignas', 'alignof', 'and', 'and_eq', 'asm', 'atomic_cancel', 'atomic_commit', 'atomic_noexcept',
    'auto', 'bitand', 'bitor', 'bool', 'break', 'case', 'catch', 'char', 'char8_t', 'char16_t', 'char32_t',
    'class', 'compl', 'concept', 'const', 'consteval', 'constexpr', 'constinit', 'const_cast', 'continue',
    'co_await', 'co_return', 'co_yield', 'decltype', 'default', 'delete', 'do', 'double', 'dynamic_cast',
    'else', 'enum', 'explicit', 'export', 'extern', 'false', 'float', 'for', 'friend', 'goto', 'if',
    'inline', 'int', 'long', 'mutable', 'namespace', 'new', 'noexcept', 'not', 'not_eq', 'nullptr',
    'operator', 'or', 'or_eq', 'private', 'protected', 'public', 'register', 'reinterpret_cast',
    'requires', 'return', 'short', 'signed', 'sizeof', 'static', 'static_assert', 'static_cast', 'struct',
    'switch', 'synchronized', 'template', 'this', 'thread_local', 'throw', 'true', 'try', 'typedef',
    'typeid', 'typename', 'union', 'unsigned', 'using', 'virtual', 'void', 'volatile', 'wchar_t',
    'while', 'xor', 'xor_eq'
];
const typeKeywords = [
        'bool', 'char', 'char16_t', 'char32_t', 'char8_t', 'double', 'float', 'int', 'long', 'short',
        'signed', 'unsigned', 'void', 'wchar_t'
    ]
const operators =  [
        '=', '>', '<', '!', '~', '?', ':', '==', '<=', '>=', '!=', '&&', '||', '++', '--', '+', '-', '*', '/', '&', '|', '^',
        '%', '<<', '>>', '>>>', '+=', '-=', '*=', '/=', '&=', '|=', '^=', '%=', '<<=', '>>='
    ]
const CppWorker =(monaco)=>{
monaco.languages.register({ id: 'cpp',
    extensions: ['.cpp', '.cc', '.cxx', '.c++', '.h', '.hh', '.hpp', '.hxx', '.h++'],
    aliases: ['C++', 'cpp'],
    mimetypes: ['text/x-c++src'],
 })

monaco.languages.setMonarchTokensProvider('cpp', {
    // 默认样式规则
    defaultToken: '',
    tokenPostfix: '.cpp',
    // 关键字列表
    keywords,
    // 数据类型
    typeKeywords,
    // 操作符
    operators,
    escapes: /\\(?:[abfnrtv\\"'0-9x])/, 
    // 词法规则
    tokenizer: {
       root: [
      // Keyword tokens for C++
      [/\b(public|private|protected|class|interface|enum|if|else|for|while|switch)\b/, 'keyword'],
      // Other rules...
      [/\b(int|float|double|char|long|void)\b/, 'type'],
      [/[A-Za-z_][\w_]*/, 'identifier'], 
      [/\d+/, 'number'], 
      [/[{}()\[\]]/, 'delimiter'] 
    ],
        // 字符串处理
        string: [
            [/[^\\"]+/, 'string'],
            [/@escapes/, 'string.escape'],
            [/\\./, 'string.escape.invalid'],
            [/"/, { token: 'string.quote', next: '@pop' }]
        ],

        // 多行注释处理
        comment: [
            [/[^\/*]+/, 'comment'],
            [/\*\//, { token: 'comment', next: '@pop' }],
            [/[\/*]/, 'comment']
        ]
    }
});
monaco.languages.registerCompletionItemProvider('cpp', {
    triggerCharacters: ['.', '>', ':', '<', '"', '#', ' '], // 自动触发补全
    provideCompletionItems: (model, position) => {
        // 将关键字转换为补全项
        const keywordSuggestions = cppKeywords.map(keyword => ({
            label: keyword,
            kind: monaco.languages.CompletionItemKind.Keyword,
            insertText: keyword,
            detail: 'C++ keyword',
            documentation: `C++ keyword: ${keyword}`
        }));

        // 添加标准库函数和操作符
        const librarySuggestions = [
            {
                label: 'std::cout',
                kind: monaco.languages.CompletionItemKind.Function,
                insertText: 'std::cout << ${1:"message"} << std::endl;',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'Output stream',
                documentation: 'Outputs data to the console using std::cout'
            },
            {
                label: 'std::cin',
                kind: monaco.languages.CompletionItemKind.Function,
                insertText: 'std::cin >> ${1:variable};',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'Input stream',
                documentation: 'Reads data from the console using std::cin'
            },
            {
                label: 'std::vector',
                kind: monaco.languages.CompletionItemKind.Class,
                insertText: 'std::vector<${1:Type}> ${2:variable};',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'STL vector',
                documentation: 'A sequence container from the C++ Standard Library'
            },
            {
                label: 'std::map',
                kind: monaco.languages.CompletionItemKind.Class,
                insertText: 'std::map<${1:KeyType}, ${2:ValueType}> ${3:variable};',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'STL map',
                documentation: 'An associative container from the C++ Standard Library'
            }
        ];

        // 添加代码片段
        const snippetSuggestions = [
            {
                label: 'main',
                kind: monaco.languages.CompletionItemKind.Snippet,
                insertText: 'int main() {\n\t${1:return 0;}\n}',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'Main function',
                documentation: 'Entry point of a C++ program'
            },
            {
                label: 'for loop',
                kind: monaco.languages.CompletionItemKind.Snippet,
                insertText: 'for (${1:int i = 0}; ${2:i < n}; ${3:++i}) {\n\t$0\n}',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'For loop',
                documentation: 'Iterative loop statement'
            },
            {
                label: 'while loop',
                kind: monaco.languages.CompletionItemKind.Snippet,
                insertText: 'while (${1:condition}) {\n\t$0\n}',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'While loop',
                documentation: 'Iterative loop statement'
            },
            {
                label: 'if-else',
                kind: monaco.languages.CompletionItemKind.Snippet,
                insertText: 'if (${1:condition}) {\n\t$0\n} else {\n\t\n}',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'If-else statement',
                documentation: 'Conditional statement'
            },
            {
                label: 'class',
                kind: monaco.languages.CompletionItemKind.Snippet,
                insertText: 'class ${1:ClassName} {\npublic:\n\t${1:ClassName}() {}\n\t~${1:ClassName}() {}\n\nprivate:\n\t$0\n};',
                insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                detail: 'Class definition',
                documentation: 'Defines a C++ class'
            }
        ];
        return {
            suggestions: [
                ...keywordSuggestions,
                ...librarySuggestions,
                ...snippetSuggestions
            ]
        };
    }
});
}
export default CppWorker;

